package cn.miao.middle.cloud.log.dto;

import java.util.Optional;

import com.github.structlog4j.IToLog;

import cn.miao.middle.cloud.core.auth.details.LoginUser;
import cn.miao.middle.cloud.core.util.SysUserUtil;
import cn.miao.middle.cloud.core.util.TokenUtil;
import cn.miao.middle.cloud.log.util.TraceUtil;
import lombok.Builder;
import lombok.Data;

/**
 * 业务日志
 * 
 * @author kongyufei
 *
 */
@Data
@Builder
public class LogEntry implements IToLog {

	/**
	 * transId
	 */
	private String transId;
	
	/**
	 * 路径
	 */
	private String path;
	
	/**
	 * 类
	 */
	private String clazz;
	
	/**
	 * 方法
	 */
	private String method;
	
	/**
	 * token
	 */
	private String token;
	
	/**
	 * 用户名
	 */
	private String username;
	
	/**
	 * 消息
	 */
	private String msg;
	
	/**
	 * 错误
	 */
	private String error;

	@Override
	public Object[] toLog() {
		return new Object[] { "transId", Optional.ofNullable(TraceUtil.getTrace()).orElse(""), "path",
				Optional.ofNullable(path).orElse(""), "clazz", Optional.ofNullable(clazz).orElse(""), "method",
				Optional.ofNullable(method).orElse(""), "token", Optional.ofNullable(TokenUtil.getToken()).orElse(""),
				"username", Optional.ofNullable(SysUserUtil.getLoginUser()).orElse(new LoginUser()).getUsername(),
				"msg", Optional.ofNullable(msg).orElse(""), "error", Optional.ofNullable(error).orElse("")

		};
	}
}